package com.xsq.page;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.jaeger.library.StatusBarUtil;
import com.xsq.Constants;
import com.xsq.R;
import com.xsq.utils.HttpUtil;
import com.xsq.utils.NetWorkUtils;
import com.xsq.utils.StatusUtils;
import com.xsq.utils.Utils;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.common.util.LogUtil;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.HashMap;

@ContentView(R.layout.activity_sync)
public class SyncCheckActivity extends BaseActivity implements View.OnClickListener {

    @ViewInject(R.id.edit_ip)
    private EditText editIp;
    @ViewInject(R.id.edit_port)
    private EditText editPort;
    @ViewInject(R.id.edit_mer_no)
    private EditText editMerId;
    @ViewInject(R.id.edit_win_no)
    private EditText editWinId;
    @ViewInject(R.id.submit_btn)
    private Button submitBtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        StatusBarUtil.setTransparent(this);

        submitBtn.setOnClickListener(this);
    }

    private void sync(String ip, String port, final String merId, final String winId) {
        final String baseUrl = "http://" + ip + ":" + port;
        HashMap<String, String> params = new HashMap<>();
        params.put("posSn", Utils.getAndroidId(this));
        params.put("posIp", NetWorkUtils.getIPAddress(this));
        params.put("posPort", port);
        params.put("merchantNo", merId);
        params.put("windowNo", winId);
        HttpUtil.getInstance().post(baseUrl + Constants.url_bindPos, params, new Callback.CommonCallback<JSONObject>() {
            @Override
            public void onSuccess(JSONObject response) {
                LogUtil.e("response==" + response.toString());
                Toast.makeText(SyncCheckActivity.this, response.optString("msg"), Toast.LENGTH_SHORT).show();
                if(response.optInt("code") == 0) {
                    setConfig(baseUrl, merId, winId);
                    Intent intent = new Intent(SyncCheckActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                StatusUtils.handleError(ex, SyncCheckActivity.this);
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submit_btn:
                /*Intent intent = new Intent(this, NFCControllerActivity.class);
                startActivity(intent);*/
                String ip = editIp.getText().toString();
                String port = editPort.getText().toString();
                String merId = editMerId.getText().toString();
                String winId = editWinId.getText().toString();
                if(TextUtils.isEmpty(ip)) {
                    Toast.makeText(this, "请输入IP", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(port)) {
                    Toast.makeText(this, "请输入Port", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(merId)) {
                    Toast.makeText(this, "请输入商户号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(winId)) {
                    Toast.makeText(this, "请输入窗口号", Toast.LENGTH_SHORT).show();
                    return;
                }
                sync(ip, port, merId, winId);
                break;
        }
    }
}
